class QueuedDownloadsController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @queued_download_pages, @queued_downloads = paginate :queued_downloads, :per_page => 10
  end

  def show
    @queued_download = QueuedDownload.find(params[:id])
  end

  def new
    @queued_download = QueuedDownload.new
  end

  def create
    @queued_download = QueuedDownload.new(params[:queued_download])
    if @queued_download.save
      flash[:notice] = 'QueuedDownload was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @queued_download = QueuedDownload.find(params[:id])
  end

  def update
    @queued_download = QueuedDownload.find(params[:id])
    if @queued_download.update_attributes(params[:queued_download])
      flash[:notice] = 'QueuedDownload was successfully updated.'
      redirect_to :action => 'show', :id => @queued_download
    else
      render :action => 'edit'
    end
  end

  def destroy
    QueuedDownload.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

  def push
    episode_id = params[:episode_id]
    @download = QueuedDownload.new
    @download.episode_id = episode_id
    @download.date_added = Time.now
    @download.save
    @episode = Episode.find(episode_id)
    @episode.torrent_status = "queued"
    @episode.save
    redirect_to :action => 'list'
  end

  def start_download
    @queued_download = QueuedDownload.find(params[:id])
    @queued_download.start_download
    redirect_to :action => 'list'
  end
end
